package handlers

import (
	"net/http"

	"dormitory/database"
	"dormitory/models"

	"github.com/gin-gonic/gin"
)

// GetDormitories 获取宿舍列表
func GetDormitories(c *gin.Context) {
	var dormitories []models.Dormitory
	result := database.DB.Find(&dormitories)
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取宿舍列表失败"})
		return
	}
	c.JSON(http.StatusOK, dormitories)
}

// CreateDormitory 创建新宿舍
func CreateDormitory(c *gin.Context) {
	var dormitory models.Dormitory
	if err := c.ShouldBindJSON(&dormitory); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"})
		return
	}

	// 检查宿舍是否已存在
	var existingDorm models.Dormitory
	if err := database.DB.Where("room_no = ?", dormitory.RoomNo).First(&existingDorm).Error; err == nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "该宿舍号已存在"})
		return
	}

	// 初始化已住人数
	dormitory.Occupied = 0

	result := database.DB.Create(&dormitory)
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "创建宿舍失败"})
		return
	}

	c.JSON(http.StatusCreated, dormitory)
}

// GetDormitory 获取单个宿舍信息
func GetDormitory(c *gin.Context) {
	id := c.Param("id")
	var dormitory models.Dormitory

	result := database.DB.First(&dormitory, id)
	if result.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "宿舍不存在"})
		return
	}

	// 获取该宿舍的学生列表
	var students []models.Student
	database.DB.Where("room_no = ?", dormitory.RoomNo).Find(&students)

	c.JSON(http.StatusOK, gin.H{
		"dormitory": dormitory,
		"students":  students,
	})
}

// UpdateDormitory 更新宿舍信息
func UpdateDormitory(c *gin.Context) {
	id := c.Param("id")
	var dormitory models.Dormitory

	// 检查宿舍是否存在
	if err := database.DB.First(&dormitory, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "宿舍不存在"})
		return
	}

	// 保存原始宿舍号
	oldRoomNo := dormitory.RoomNo

	// 绑定更新数据
	if err := c.ShouldBindJSON(&dormitory); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"})
		return
	}

	// 如果宿舍号改变，检查新宿舍号是否已存在
	if oldRoomNo != dormitory.RoomNo {
		var existingDorm models.Dormitory
		if err := database.DB.Where("room_no = ?", dormitory.RoomNo).First(&existingDorm).Error; err == nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "新宿舍号已存在"})
			return
		}
	}

	// 更新宿舍信息
	if err := database.DB.Save(&dormitory).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "更新宿舍信息失败"})
		return
	}

	// 如果宿舍号改变，更新相关学生的宿舍号
	if oldRoomNo != dormitory.RoomNo {
		database.DB.Model(&models.Student{}).Where("room_no = ?", oldRoomNo).Update("room_no", dormitory.RoomNo)
	}

	c.JSON(http.StatusOK, dormitory)
}

// DeleteDormitory 删除宿舍
func DeleteDormitory(c *gin.Context) {
	id := c.Param("id")
	var dormitory models.Dormitory

	// 先获取宿舍信息
	if err := database.DB.First(&dormitory, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "宿舍不存在"})
		return
	}

	// 检查是否有学生住在该宿舍
	var count int64
	database.DB.Model(&models.Student{}).Where("room_no = ?", dormitory.RoomNo).Count(&count)
	if count > 0 {
		c.JSON(http.StatusBadRequest, gin.H{"error": "该宿舍还有学生居住，无法删除"})
		return
	}

	// 删除宿舍
	result := database.DB.Delete(&dormitory)
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除宿舍失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "宿舍已删除"})
}
